package com.mycjj.android.activity;

import static java.util.Calendar.MINUTE;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.mycjj.android.R;
import com.mycjj.android.bean.YuyueMonthStateBean;
import com.mycjj.android.bean.YuyueMonthStateBean.YuyueData;
import com.mycjj.android.http.ProjectHttpRequestImpl;
import com.mycjj.android.http.ProjectHttpRequestInterface;
import com.mycjj.android.util.Preferences;
import com.mycjj.android.util.Utils;
import com.squareup.timessquare.CalendarPickerView;
import com.squareup.timessquare.CalendarPickerView.OnDateSelectedListener;
import com.squareup.timessquare.CalendarPickerView.SelectionMode;

public class SelectYuyueDateActivity extends BaseActivity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_select_yuyue_date);
		mActManager.pushActivity(YUYUE_KEY, this);
		initHeader();
		init();
	}

	private ImageView mHeaderLeftImageview;
	private Button mHeaderRightButton;
	private TextView mHeaderTitleTextView;

	private void initHeader() {
		mHeaderLeftImageview = (ImageView) findViewById(R.id.header_left_button);
		mHeaderLeftImageview.setImageResource(R.drawable.back);
		mHeaderRightButton = (Button) findViewById(R.id.header_right_button);
		mHeaderRightButton.setVisibility(View.GONE);
		mHeaderTitleTextView = (TextView) findViewById(R.id.header_title_textview);
		mHeaderTitleTextView.setText("选择日期");
		mHeaderLeftImageview.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				mActManager.popActivity(YUYUE_KEY, SelectYuyueDateActivity.this);
			}
		});
	}

	private CalendarPickerView calendar;
	private TextView mMouthTextView;
	private TextView mTv_store_name;
	private ImageView mLeftButton, mRightButton;
	private Button mSureButton;

	private Calendar mFirstDateOfMonthCal;
	private Calendar mLastDateOfMonthCal;
	private Calendar mCurrentDateCal;
	Date date = new Date();
	private String type;
	private String carId;
	private List<YuyueData> dataList;
	@SuppressWarnings("unchecked")
	private void init() {
		Intent intent = getIntent();
		if(intent != null){
			type = intent.getStringExtra("type");
			dataList = (ArrayList<YuyueData>)intent.getSerializableExtra("dataList");
			carId = intent.getStringExtra("carId");
		}
		mTv_store_name = (TextView)findViewById(R.id.tv_store_name);
		calendar = (CalendarPickerView) findViewById(R.id.calendar_view);
		calendar.setSelection(0);
		calendar.setSelectionAfterHeaderView();
		mMouthTextView = (TextView) findViewById(R.id.mouth_textview);
		mLeftButton = (ImageView) findViewById(R.id.left_button);
		mRightButton = (ImageView) findViewById(R.id.right_button);
		mLeftButton.setVisibility(View.INVISIBLE);

		mShareDate = Utils.getFormateDate(date, "yyyy-MM-dd");
		mCurrentDateCal = Calendar.getInstance();
		mCurrentDateCal.setTime(date);

		mFirstDateOfMonthCal = Calendar.getInstance();
		mFirstDateOfMonthCal.setTime(date);
		mLastDateOfMonthCal = Utils.lastDayOfMonth(date);
		calendar.initSelect(mFirstDateOfMonthCal.getTime(),
				mLastDateOfMonthCal.getTime(),dataList) //
				.inMode(SelectionMode.SINGLE) //
				.withSelectedDate(date);
		calendar.setOnDateSelectedListener(new OnDateSelectedListener() {

			@Override
			public void onDateUnselected(Date date) {

			}

			@Override
			public void onDateSelected(Date date) {
				Log.d("mark",
						"format:" + Utils.getFormateDate(date, "yyyy-MM-dd"));
				mShareDate = Utils.getFormateDate(date, "yyyy-MM-dd");
				Intent intent = new Intent(SelectYuyueDateActivity.this,SelectYuyueTimeActivity.class);
				intent.putExtra("type", type);
				intent.putExtra("date", mShareDate);
				intent.putExtra("carId", carId);
				startActivity(intent);
			}
		});
		mMouthTextView.setText(mFirstDateOfMonthCal.get(Calendar.YEAR)+"年"+Utils.getMonth(mFirstDateOfMonthCal.get(Calendar.MONTH) + 1) + " 月");
		mLeftButton.setOnClickListener(monthClickListener);
		mRightButton.setOnClickListener(monthClickListener);
	}
	@Override
	protected void onResume() {
		super.onResume();
		mTv_store_name.setText(Preferences.getSharedPreferences(mApp, "storeName", "浙江奥同汽车有限公司"));
	}
	private void getPlanState(String storeId,String month){
		RequestParams params = new RequestParams();
		params.put("store_id", storeId);
		params.put("month", month);
		params.put("client_id",ProjectHttpRequestInterface.client_id_key);
		params.put("token", Preferences.getSharedPreferences(this, "user_token", ""));
		request = new ProjectHttpRequestImpl(this, true);
		request.requestStoreGetMonthPlanWithDoneHandler("", params, new AsyncHttpResponseHandler(){
			@Override
			public void onSuccess(String content) {
				Log.i("info", "----ethan==="+content);
				YuyueMonthStateBean bean = dataManager.getYuyueMonthStateBean();
				if(isCurrentMonth){//当前月份
					calendar.initSelect(mFirstDateOfMonthCal.getTime(),
							mLastDateOfMonthCal.getTime(),bean.getData()) //
							.inMode(SelectionMode.SINGLE) //
							.withSelectedDate(date);
				}else{
					calendar.initSelect(mFirstDateOfMonthCal.getTime(),
							mLastDateOfMonthCal.getTime(),bean.getData()).inMode(
							SelectionMode.SINGLE);
				}
			}
		});
	}
	
	
	private OnClickListener monthClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.left_button:
				mFirstDateOfMonthCal.add(Calendar.MONTH, -1);
				if (Utils.isMonthEquals(mFirstDateOfMonthCal, mCurrentDateCal,mCurrentDateCal)) {
					mFirstDateOfMonthCal.setTime(date);
				} else {
					mFirstDateOfMonthCal.set(Calendar.DAY_OF_MONTH, 1);
				}
				mLastDateOfMonthCal = Utils.lastDayOfMonth(mFirstDateOfMonthCal
						.getTime());
				break;
			case R.id.right_button:
				mFirstDateOfMonthCal.add(Calendar.MONTH, +1);
				mFirstDateOfMonthCal.set(Calendar.DAY_OF_MONTH, 1);
				mLastDateOfMonthCal.add(Calendar.MONTH, +2);
				mLastDateOfMonthCal.set(Calendar.DAY_OF_MONTH, 0);
				break;
			}
			mRightButton.setVisibility(View.VISIBLE);
			if (Utils.isMonthEquals(mFirstDateOfMonthCal, mLastDateOfMonthCal,mCurrentDateCal)) {
				mLeftButton.setVisibility(View.INVISIBLE);
				isCurrentMonth = true;
			} else {
				mLeftButton.setVisibility(View.VISIBLE);
				isCurrentMonth = false;
			}
			mMouthTextView.setText(mFirstDateOfMonthCal.get(Calendar.YEAR)+"年"+ Utils.getMonth(mFirstDateOfMonthCal.get(Calendar.MONTH) + 1) + " 月");
			
			String storeid = Preferences.getSharedPreferences(SelectYuyueDateActivity.this,"storeId", "");
			String now_date = mFirstDateOfMonthCal.get(Calendar.YEAR)+"-"+Utils.getMonth(mFirstDateOfMonthCal.get(Calendar.MONTH) + 1);
			getPlanState(storeid,now_date);
		}
	};
	private boolean isCurrentMonth = true;
	private static String mShareDate;
}
